package com.glory.brepda.activity.mfg;


import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.base.Constant;
import com.glory.brepda.entity.DefectCodeEntity;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.LotLists;
import com.glory.brepda.entity.ScrapEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.LotUtils;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


public class DSInspectActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.tvEqpWork)
    TextView tvEqpWork;
    @BindView(R.id.tvStep)
    TextView tvStep;
    @BindView(R.id.tvSliceNum)
    TextView tvSliceNum;
//    @BindView(R.id.etStorageid)
//    FreeUI_ClearEditText etStorageid;
    @BindView(R.id.etSliceLot)
    TextView etSliceLot;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvLotState)
    TextView tvLotState;
    @BindView(R.id.rbNo)
    RadioButton rbNo;
    @BindView(R.id.rbYes)
    RadioButton rbYes;
    @BindView(R.id.flPositionList)
    FrameLayout flPositionList;
    @BindView(R.id.ivPositionArrow)
    ImageView ivPositionArrow;
    @BindView(R.id.lin_flPositionList)
    LinearLayout lin_flPositionList;
    @BindView(R.id.lin_Storageid)
    LinearLayout lin_Storageid;
    @BindView(R.id.tvEqpType)
    TextView tvEqpType;
    private List<String> mMaterielList;
    private int mState = 2;
    private boolean ISABNORMAL = false;
    private FreeUI_GeneralPop mMaterielPop;
    private LotInfoEntity lotInfoEntity;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_rowinspecterror;
    }

    @Override
    protected void initView() {
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        setMainLayout(llMain);
        tvBaseSubmit.setVisibility(View.GONE);
        etLot.requestFocus();
        mMaterielList = new ArrayList<>();
        mMaterielList.add("04001/一分厂线边仓");
        mMaterielList.add("04002/二分厂线边仓");
        mMaterielList.add("04003/三分厂线边仓");

        initEdit(etLot, s -> {
            LotUtils.searchLotInfo(DSInspectActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {

                @Override
                public void onSuccess(LotInfoEntity response) {

                    lotInfoEntity = response;
                    tvLotState.setText(response.getLOT().getSTATE());
                    tvEqpWork.setText(response.getLOT().getEQUIPMENTID());
                    etSliceLot.setText(response.getLOT().getCUSTOMERLOTID());
                    tvSliceNum.setText(response.getLOT().getSUBQTY());
                    tvStep.setText(response.getLOT().getSTEPNAME() + "/" + response.getLOT().getSTEPDESC());
                    baseSubmit();
                }

                @Override
                public void onFail(Throwable t) {
                    loadError(t, "LOTQUERY");
                }
            });

        });

    }

    private void baseSubmit() {
        if (getText(etLot).equals("")) {
            showToast("请先扫描批次信息", false);
            return;
        }
        if (tvLotState.getText().toString().equals("待投产")) {
            showConfirmDialog("批次未投产，请联系计划部执行投产");
            return;
        }
        if (!rbNo.isChecked() && !rbYes.isChecked()){
            showConfirmDialog("请选择是否异常");
            return;
        }

        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<JSONObject>>
                (new TypeToken<BaseResponseBean<JSONObject>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    showToast("成功", true);
                    etLot.requestFocus();
                    etLot.setText("");
                    tvEqpWork.setText("");
                    tvStep.setText("");
                    tvLotState.setText("");
                    etSliceLot.setText("");
                    tvSliceNum.setText("");
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                super.onError(response);
                loadError(response.getException(), "LOTSCRIBEINSPECTIONTRACKIN");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE", "BIND");
        map.addProperty("EQUIPMENTID", tvEqpWork.getText().toString());
        map.addProperty("WAREHOUSEID", tvEqpType.getText().toString().equals("")?"":tvEqpType.getText().toString().substring(0, 5));
        map.addProperty("STORAGEID", "");
        map.addProperty("ISABNORMAL", ISABNORMAL);
        LotLists lotLists = new LotLists(etLot.getText().toString());
        List<LotLists> lotList = new ArrayList<>();
        lotList.add(lotLists);
        map.add("LOTLIST", new JsonParser().parse(new Gson().toJson(lotList)).getAsJsonArray());
        NetUtils.requestPostNet("IF.LOTSCRIBEINSPECTIONTRACKIN", "Common", DSInspectActivity.this, map, callBack);

    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvBaseSubmit,R.id.rbYes,R.id.rbNo, R.id.flPositionList})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
//                if (actionControl("Submit02")) {
//                    return;
//                }
                if (getText(etLot).equals("")) {
                    showToast("请先扫描批次信息", false);
                    return;
                }

                if (tvLotState.getText().toString().equals("待投产")) {
                    showConfirmDialog("批次未投产，请联系计划部执行投产");
                    return;
                }

//                if (tvLotState.getText().toString().equals("等待作业")) {
//                    showConfirmDialog("批次没有作业，请操作进站功能");
//                    return;
//                }

                if (!rbNo.isChecked() && !rbYes.isChecked()){
                    showConfirmDialog("请选择是否异常");
                    return;
                }

//                if (ISABNORMAL){
//                    tvEqpType.setText("");
//                    etStorageid.setText("");
//                }else {
//                    if (tvEqpType.getText().toString().equals("") || etStorageid.getText().toString().equals("")){
//                        showToast("仓库号或库位不能为空", false);
//                        return;
//                    }
//                }


                FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<JSONObject>>
                        (new TypeToken<BaseResponseBean<JSONObject>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                            showToast("成功", true);
                            etLot.requestFocus();
                            etLot.setText("");
                            tvEqpWork.setText("");
                            tvStep.setText("");
                            tvLotState.setText("");
                            etSliceLot.setText("");
                            tvSliceNum.setText("");
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<JSONObject>> response) {
                        super.onError(response);
                        loadError(response.getException(), "LOTSCRIBEINSPECTIONTRACKIN");
                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE", "BIND");
                map.addProperty("EQUIPMENTID", tvEqpWork.getText().toString());
                map.addProperty("WAREHOUSEID", tvEqpType.getText().toString().equals("")?"":tvEqpType.getText().toString().substring(0, 5));
                map.addProperty("STORAGEID", "");
                map.addProperty("ISABNORMAL", ISABNORMAL);
                LotLists lotLists = new LotLists(etLot.getText().toString());
                List<LotLists> lotList = new ArrayList<>();
                lotList.add(lotLists);
                map.add("LOTLIST", new JsonParser().parse(new Gson().toJson(lotList)).getAsJsonArray());
                NetUtils.requestPostNet("IF.LOTSCRIBEINSPECTIONTRACKIN", "Common", DSInspectActivity.this, map, callBack);

                break;
            case R.id.rbNo:
//                if (tvEqpType.getText().toString().equals("") || etStorageid.getText().toString().equals("")) {
//                    showToast("请输入仓库号和库位", false);
//                }

//                lin_flPositionList.setVisibility(View.VISIBLE);
//                lin_Storageid.setVisibility(View.VISIBLE);
                ISABNORMAL = false;
                break;
            case R.id.rbYes:
                tvEqpType.setText("");
//                etStorageid.setText("");
                lin_flPositionList.setVisibility(View.GONE);
                lin_Storageid.setVisibility(View.GONE);
                ISABNORMAL = true;
                break;
            case R.id.flPositionList:
                mMaterielPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flPositionList.getWidth(), mMaterielList, ivPositionArrow, (codeS, i) -> {
                    tvEqpType.setText(codeS);
                });
                FreeApi_PopUtils.setArrowShow(ivPositionArrow);
                mMaterielPop.showAsDropDown(flPositionList, 0, 0);
                break;
        }

    }
}